import { IModel, IProxy } from "../Interfaces";

export class Model implements IModel {

  proxyMap: Object = null;
  multitonKey: string = null;
  static MULITON_MSG: string = "Model instance for this multiton uid already contructed";
  static instanceMap: Object = {};
  static getInstance(key): IModel {
    if (!Model.instanceMap[key])
      Model.instanceMap[key] = new Model(key);
    return Model.instanceMap[key];
  }
  static removeModel(key: string): void {

    delete Model.instanceMap[key];
  }
  constructor(key: string) {
    if (Model.instanceMap[key])
      throw Error(Model.MULITON_MSG);
    Model.instanceMap[key] = this;
    this.multitonKey = key;
    this.proxyMap = {};
    this.initializeModel();
  }

  initializeModel(): void {

  }

  registerProxy(proxy: IProxy): void {
    proxy.initializeNotifier(this.multitonKey);
    this.proxyMap[proxy.getProxyName()] = proxy;
    proxy.onRegister();
  }

  removeProxy(proxyName: string): IProxy {
    let proxy: IProxy = this.proxyMap[proxyName];
    if (proxy) {

      delete this.proxyMap[proxyName];
      proxy.onRemove();
    }
    return proxy;
  }

  retrieveProxy(proxyName: string): IProxy {

    return this.proxyMap[proxyName] || null;
  }

  hasProxy(proxyName: string): boolean {

    return this.proxyMap[proxyName] != null;
  }
}